const Router = require('@koa/router');
const { checkAuth, checkPermission } = require('../middlewares/auth');
const pointRuleController = require('../controllers/pointRule.controller');

const router = new Router({ prefix: '/api/v1/point-rules' });

/**
 * @swagger
 * /api/v1/point-rules:
 *   post:
 *     tags: [积分规则]
 *     summary: 创建积分规则
 *     description: 创建新的积分获取或消费规则
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - type
 *               - points
 *             properties:
 *               name:
 *                 type: string
 *                 description: 规则名称
 *                 example: "每日签到"
 *               type:
 *                 type: string
 *                 enum: [earn, spend]
 *                 description: 规则类型（获取/消费）
 *                 example: "earn"
 *               points:
 *                 type: integer
 *                 description: 积分数量
 *                 example: 10
 *               conditions:
 *                 type: object
 *                 description: 规则触发条件
 *                 example: {
 *                   "frequency": "daily",
 *                   "minAmount": 0,
 *                   "maxAmount": 0
 *                 }
 *               description:
 *                 type: string
 *                 description: 规则说明
 *                 example: "每日签到可获得10积分"
 *               validFrom:
 *                 type: string
 *                 format: date-time
 *                 description: 规则生效时间
 *                 example: "2024-01-01T00:00:00Z"
 *               validTo:
 *                 type: string
 *                 format: date-time
 *                 description: 规则失效时间
 *                 example: "2024-12-31T23:59:59Z"
 *               dailyLimit:
 *                 type: integer
 *                 description: 每日限制次数
 *                 example: 1
 *               monthlyLimit:
 *                 type: integer
 *                 description: 每月限制次数
 *                 example: 30
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "rule_001"
 *                 message:
 *                   type: string
 *                   example: "积分规则创建成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/', checkAuth, checkPermission('point_rule:create'), pointRuleController.createPointRule);

/**
 * @swagger
 * /api/v1/point-rules:
 *   get:
 *     tags: [积分规则]
 *     summary: 获取积分规则列表
 *     description: 获取所有积分规则（支持类型和状态筛选）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [earn, spend]
 *         description: 规则类型筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [active, inactive]
 *         description: 规则状态筛选
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 100
 *                 pages:
 *                   type: integer
 *                   example: 10
 *                 current:
 *                   type: integer
 *                   example: 1
 *                 rules:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "rule_001"
 *                       name:
 *                         type: string
 *                         example: "每日签到"
 *                       type:
 *                         type: string
 *                         example: "earn"
 *                       points:
 *                         type: integer
 *                         example: 10
 *                       status:
 *                         type: string
 *                         example: "active"
 *                       usageCount:
 *                         type: integer
 *                         example: 1000
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/', checkAuth, checkPermission('point_rule:read'), pointRuleController.getPointRules);

/**
 * @swagger
 * /api/v1/point-rules/active:
 *   get:
 *     tags: [积分规则]
 *     summary: 获取活动规则列表
 *     description: 获取所有当前生效的积分规则
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 earnRules:
 *                   type: array
 *                   description: 积分获取规则
 *                   items:
 *                     $ref: '#/components/schemas/PointRule'
 *                 spendRules:
 *                   type: array
 *                   description: 积分消费规则
 *                   items:
 *                     $ref: '#/components/schemas/PointRule'
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/active', checkAuth, checkPermission('point_rule:read'), pointRuleController.getActiveRules);

/**
 * @swagger
 * /api/v1/point-rules/{id}:
 *   get:
 *     tags: [积分规则]
 *     summary: 获取规则详情
 *     description: 获取指定积分规则的详细信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 规则ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "rule_001"
 *                 name:
 *                   type: string
 *                   example: "每日签到"
 *                 type:
 *                   type: string
 *                   example: "earn"
 *                 points:
 *                   type: integer
 *                   example: 10
 *                 conditions:
 *                   type: object
 *                   example: {
 *                     "frequency": "daily",
 *                     "minAmount": 0,
 *                     "maxAmount": 0
 *                   }
 *                 description:
 *                   type: string
 *                   example: "每日签到可获得10积分"
 *                 validFrom:
 *                   type: string
 *                   format: date-time
 *                 validTo:
 *                   type: string
 *                   format: date-time
 *                 dailyLimit:
 *                   type: integer
 *                   example: 1
 *                 monthlyLimit:
 *                   type: integer
 *                   example: 30
 *                 status:
 *                   type: string
 *                   example: "active"
 *                 usageCount:
 *                   type: integer
 *                   example: 1000
 *                 createdAt:
 *                   type: string
 *                   format: date-time
 *                 updatedAt:
 *                   type: string
 *                   format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 规则不存在
 */
router.get('/:id', checkAuth, checkPermission('point_rule:read'), pointRuleController.getPointRule);

/**
 * @swagger
 * /api/v1/point-rules/{id}:
 *   put:
 *     tags: [积分规则]
 *     summary: 更新积分规则
 *     description: 更新指定积分规则的配置信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 规则ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 规则名称
 *               points:
 *                 type: integer
 *                 description: 积分数量
 *               conditions:
 *                 type: object
 *                 description: 规则触发条件
 *               description:
 *                 type: string
 *                 description: 规则说明
 *               validFrom:
 *                 type: string
 *                 format: date-time
 *                 description: 规则生效时间
 *               validTo:
 *                 type: string
 *                 format: date-time
 *                 description: 规则失效时间
 *               dailyLimit:
 *                 type: integer
 *                 description: 每日限制次数
 *               monthlyLimit:
 *                 type: integer
 *                 description: 每月限制次数
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "积分规则更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 规则不存在
 */
router.put('/:id', checkAuth, checkPermission('point_rule:update'), pointRuleController.updatePointRule);

/**
 * @swagger
 * /api/v1/point-rules/{id}:
 *   delete:
 *     tags: [积分规则]
 *     summary: 删除积分规则
 *     description: 删除指定的积分规则
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 规则ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "积分规则删除成功"
 *       400:
 *         description: 无法删除（规则正在使用中）
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 规则不存在
 */
router.delete('/:id', checkAuth, checkPermission('point_rule:delete'), pointRuleController.deletePointRule);

/**
 * @swagger
 * /api/v1/point-rules/{id}/status:
 *   patch:
 *     tags: [积分规则]
 *     summary: 更新规则状态
 *     description: 更新指定积分规则的状态（启用/停用）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 规则ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [active, inactive]
 *                 description: 新的状态（活跃/停用）
 *     responses:
 *       200:
 *         description: 状态更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "积分规则状态更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 规则不存在
 */
router.patch('/:id/status', checkAuth, checkPermission('point_rule:update'), pointRuleController.updatePointRuleStatus);

module.exports = router; 